<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<%@taglib uri="/authtaglib" prefix="auth" %>
<div id="cc" class="easyui-layout" style="height:500px;">
    <div data-options="region:'west',title:'可选报警级别',split:true" style="width:350px;">
        <table id="datagrid_regioninfo"></table>
    </div>
    <div data-options="region:'center',title:'已选报警级别',split:true">
        <table id="datagrid_regioninfo2"></table>
    </div>
</div>

<script type="text/javascript">
    Array.prototype.contains = function(item){
        return RegExp(item).test(this);
    };
    Array.prototype.remove = function(value) {
        var index = this.indexOf(value);
        if(index != -1){
            this.splice(index, 1);
        }
    };

    //页面加载完后响应 onReady
    //初始化主列表
    var checkedIds = [];
    $('#datagrid_regioninfo').datagrid({
        url: '${pageContext.request.contextPath}/cnbdw/regionInfoAction!getAllDatasourceList.action',
        fit: true,
        border: false,
        fitColumns: true,
        pagination: true,
        height: 500,
        pageList: [10, 20, 30],
        rownumbers: false,
        sortName: 'id',
        sortOrder: 'desc',
        striped: true,
        columns: [
            [
                {field: 'ck', checkbox: true},
                {field: 'name', title: '报警级别名称', align: 'left', formatter: function (value, rowData, rowIndex) {if (value == '') {return '--'} return value;}},
                {field: 'createTime', title: '创建时间', align: 'left', formatter: function (value, rowData, rowIndex) {if (value == '') {return '--'} return value;}},
                {field: 'createUser', title: '创建人员', align: 'left', formatter: function (value, rowData, rowIndex) {if (value == '') {return '--'} return value;}},
                {field: 'updateTime', title: '修改时间', align: 'left', formatter: function (value, rowData, rowIndex) {if (value == '') {return '--'} return value;}},
                {field: 'updateUser', title: '修改人员', align: 'left', formatter: function (value, rowData, rowIndex) {if (value == '') {return '--'} return value;}}
            ]
        ],
        onCheck: function (index, item) {
            if(checkedIds.contains(item.id)){
                return;
            }
            checkedIds.push(item.id);
            $('#datagrid_regioninfo2').datagrid('appendRow', {
                id: item.id,
                name: item.name,
                createTime: item.createTime,
                createUser: item.createUser,
                updateTime: item.updateTime,
                updateUser: item.updateUser
            });

            $('#datagrid_regioninfo2').datagrid('checkAll');
        },
        onUncheck: function (ix, row) {
            checkedIds.remove(row.id);

            var rows = $('#datagrid_regioninfo2').datagrid('getRows');
            $.each(rows, function (index, item) {
                if (item && row && item.id == row.id) {
                    $('#datagrid_regioninfo2').datagrid('deleteRow', index);
                }
            });
        },
        onCheckAll: function (rows) {
            $.each(rows, function (index, item) {
                if(checkedIds.contains(item.id)){
                    return;
                }

                checkedIds.push(item.id);
                $('#datagrid_regioninfo2').datagrid('appendRow', {
                	id: item.id,
                    name: item.name,
                    createTime: item.createTime,
                    createUser: item.createUser,
                    updateTime: item.updateTime,
                    updateUser: item.updateUser
                });
            });
            $('#datagrid_regioninfo2').datagrid('checkAll');
        },
        onUncheckAll: function (rows) {
            $.each(rows, function (ix, item) {
                checkedIds.remove(item.id);

                var getRows = $('#datagrid_regioninfo2').datagrid('getRows');
                $.each(getRows, function (index, row) {
                    if (item && row && item.id == row.id) {
                        $('#datagrid_regioninfo2').datagrid('deleteRow', index);
                    }
                });
            });
        },
        onLoadSuccess:function(data1){
            var data = $('#datagrid_regioninfo').datagrid('getRows');
            var getRows = $('#datagrid_regioninfo2').datagrid('getRows');
            $.each(data, function (ix, item) {
                $.each(getRows, function (index, row) {
                    if (item && row && item.id == row.id) {
                        $('#datagrid_regioninfo').datagrid('checkRow', ix);
                    }
                });
            });
        }
    });

    $('#datagrid_regioninfo2').datagrid({
    	fit: true,
        border: false,
        fitColumns: true,
        pagination: false,
        url: '${pageContext.request.contextPath}/cnbdw/levelAction!getRegionRelation.action?levelId=<%=request.getParameter("levelId")%>',
        height: 500,
//        pageList: [10, 20, 30],
        rownumbers: false,
        sortName: 'id',
        sortOrder: 'desc',
        striped: true,
        columns: [
            [
                {field: 'ck', checkbox: true},
                {field: 'name', title: '报警级别名称', align: 'left', hidden: false, formatter: function (value, rowData, rowIndex) {
                    if (value == '') {
                        return '--'
                    }
                    return value;
                }},
                {field: 'createTime', title: '创建时间', align: 'left', formatter: function (value, rowData, rowIndex) {
                    if (value == '') {
                        return '--'
                    }
                    return value;
                }},
                {field: 'createUser', title: '创建人员', align: 'left', formatter: function (value, rowData, rowIndex) {
                	 if (value == '') {
                         return '--'
                     }
                    return value;
                }}
//                {field: 'updateTime', title: '修改时间', align: 'left', formatter: function (value, rowData, rowIndex) {
//                    if (value == '') {
//                        return '--'
//                    }
//                    return value;
//                }},
//                {field: 'updateUser', title: '修改人员', align: 'left', formatter: function (value, rowData, rowIndex) {
//                    if (value == '') {
//                        return '--'
//                    }
//                    return value;
//                }}
            ]
        ],
        onCheck: function (index, row) {

        },
        onUncheck: function (index, row) {
            checkedIds.remove(row.id);

            var getRows = $('#datagrid_regioninfo').datagrid('getRows');
            $.each(getRows, function (ix, row1) {
                if (row1 && row && row1.id == row.id) {
                    $('#datagrid_regioninfo').datagrid('uncheckRow', ix);
                }
            });
        },
        onCheckAll: function (rows) {

        },
        onUncheckAll: function (rows) {
            checkedIds = [];
            $('#datagrid_regioninfo').datagrid("uncheckAll");
        },
        onLoadSuccess:function(data){
            if(!data.rows){
                return;
            }
            $('#datagrid_regioninfo2').datagrid("checkAll");

            var getRows = $('#datagrid_regioninfo').datagrid('getRows');
            $.each(data.rows, function (ix, item) {
                checkedIds.push(item.id);

                $.each(getRows, function (index, row) {
                    if (item && row && item.id == row.id) {
                        $('#datagrid_regioninfo').datagrid('checkRow', index);
                    }
                });
            });
        }
    });
</script>

